<?php

System::load_app_class('base','mobileapi','no');
//System::load_app_fun('my');
//System::load_app_fun('user');
//System::load_sys_fun('user');

class home extends base{
    public function __construct() {
        parent::__construct();
        $this->db=System::load_sys_class('model');
    }

    public function unserializeArray($array){
        foreach ($array as $key => $value) {
            $array[$key]['ratio'] = '16:9';
            if(array_key_exists('info',$value))			{$array[$key]['info']=unserialize($value['info']);}
            if(array_key_exists('q_user',$value))		{$array[$key]['q_user']=unserialize($value['q_user']);}
            if(array_key_exists('picarr',$value))		{$array[$key]['picarr']=unserialize($value['picarr']);}
            if(array_key_exists('q_content',$value))	{$array[$key]['q_content']=unserialize($value['q_content']);}
        }
        return $array;
    }
//首页
    public function init(){
        $cateid = $_POST['categoryId'];

        //首页轮播
        $banners=$this->db->GetList("select * from `@#_wap`");
        $index = 0;
        foreach($banners as $key => $value){
//            $banners[$key]['ratio'] = '16:9';//比例
            $linkArray =explode("/", $value['link']);//链接
            //(活动说明或商品或店铺，对应于0，1，2)
            if($linkArray[0] == '' && $linkArray[1] == 'mobile' && $linkArray[2] == 'mobile' && $linkArray[3] == 'item'){
                $banners[$key]['type'] = 1;
                $banners[$key]['id'] = $linkArray[4];

            }
            else {
                $banners[$key]['type'] = 2;
                $banners[$key]['id'] = null;
            }
            $banners[$key]['index'] = $index++;
        }


        $shoplist=$this->getCommodityList(1);
        //限时抢购
//        $lottery=$this->db->GetList("select `thumb`,`id`,`sid`,`title` from `@#_shoplist` where `state` = 1 AND `q_end_time`IS NULL ORDER BY `time` DESC LIMIT 10");
//        $lottery=$this->unserializeArray($lottery);
        $info = array('banners' =>$banners,'shoplist'=>$shoplist);//,'flashsale'=>$lottery);
        $this->success($info);
    }
    public function getCommodityList($def){

        if($def){
            $shoplist0 = $this->db->GetList("SELECT `id`,`sid`,`zongrenshu`,`canyurenshu`,`shenyurenshu`,`thumb`,`title` FROM `@#_shoplist` WHERE `q_end_time` IS NULL  order by `time` DESC LIMIT 10");
            $shoplist1 = $this->db->GetList("SELECT `id`,`sid`,`zongrenshu`,`canyurenshu`,`shenyurenshu`,`thumb`,`title` FROM `@#_shoplist` WHERE `q_end_time` IS NULL  order by `qishu` DESC LIMIT 10");
            $shoplist2 = $this->db->GetList("SELECT `id`,`sid`,`zongrenshu`,`canyurenshu`,`shenyurenshu`,`thumb`,`title` FROM `@#_shoplist` WHERE `q_end_time` IS NULL  order by `zongrenshu` DESC LIMIT 10");
        }
        if($def){
            return array("type0"=>$shoplist0,"type1" => $shoplist1,"type2"=>$shoplist2);
        }
        $cate_band = $_POST['categoryId'];
        $select =$_POST['sortMode'] ? $_POST['sortMode'] : 0;
        $pagenum = isset($_POST['page']) ? $_POST['page'] : 1;
        $this->success($_POST);
        //分页
        $end=10;
        $select_w = '';
        if(isset($cate_band)){
            $select_w = 'and `cateid` ='.$cate_band.' ';
        }
        if($select == 0){//全部商品
            $select_w .= ' order by `time` DESC';
        }
        if($select == 1){//购买最多
            $select_w .= ' order by `qishu` DESC';
        }
        if($select == 2){//最易中奖
            $select_w .= " order by `zongrenshu` DESC";
        }
//$this->success(isset($cate_band));
        $count = $this->db->GetCount("SELECT `id` FROM `@#_shoplist` WHERE `q_end_time` is NULL $select_w");

        $page = System::load_sys_class('page');

        $page->config($count, $end, $pagenum, "0");
        $shoplist = $this->db->GetPage("SELECT `id`,`sid`,`zongrenshu`,`canyurenshu`,`shenyurenshu`,`thumb`,`title` FROM `@#_shoplist` WHERE `q_end_time` IS NULL  $select_w",array("num" => $end, "page" => $pagenum, "type" => 1, "cache" => 0));
        $this_time = time();
        if(count($shoplist) > 1){
            if($shoplist[0]['time'] > $this_time - 86400*3)
                $shoplist[0]['t_new_goods'] = 1;
        }
        $pagex=ceil($count/$end);
        $shoplist=$this->unserializeArray($shoplist);
        $isNoDta = false;
        if($pagex <= $pagenum || count($shoplist)<=1){
            $isNoDta = true;
        }

        $info=array("shoplist"=>$shoplist,"isNoData" => $isNoDta,"sortMode"=>$select);
        $this->success($info);
    }
    public function getSession()
    {
        var_dump($_SESSION);
    }


}